package com.heo.util;

import java.util.Random;

import org.springframework.util.StringUtils;

public class RandomNumHelper
{
  /**
   * 检查密码是否符合要求，4位，只能是数字+大小写字母的组合
   * 
   * @param password
   * @return
   */
  public static boolean checkPassword(String password)
  {
    if (StringUtils.isEmpty(password))
    {
      return false;
    }
    if (password.length() != 4)
    {
      return false;
    }
    byte[] passArray = password.getBytes();
    for (int i = 0; i < passArray.length; i++)
    {
      if (passArray[i] <= 47)
      {
        return false;
      }
      else if (passArray[i] >= 58 && passArray[i] <= 64)
      {
        return false;
      }
      else if (passArray[i] >= 91 && passArray[i] <= 96)
      {
        return false;
      }
      else if (passArray[i] >= 123)
      {
        return false;
      }
      else
      {
        continue;
      }
    }
    return true;
  }

  /**
   * 获得指定范围内的随机数
   * 
   * @param min
   * @param max
   * @return
   */
  public static int getRangeRandomDec(int min, int max)
  {
    return (int) (min + Math.random() * (max));
  }

  /**
   * 获得指定长度的随机10进制数字
   * 
   * @param length
   * @return
   */
  public static String getRandomDecString(int length)
  {
    String base = "0123456789";
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < length; i++)
    {
      int number = random.nextInt(base.length());
      sb.append(base.charAt(number));
    }
    return sb.toString();
  }

  /**
   * 生成一个16进制的字符串，并且不以0开头
   * 
   * @param length
   * @return
   */
  public static String getRandom16HexString(int length)
  {
    String base = "ABCDEF0123456789";
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < length; i++)
    {
      int number = random.nextInt(base.length());
      sb.append(base.charAt(number));
    }
    if (sb.toString().startsWith("0"))
    {
      return "1" + sb.toString().substring(1);
    }
    else
    {
      return sb.toString();
    }
  }

  /**
   * 生成一个不含0的随机字符串
   * 
   * @param length
   * @return
   */
  public static String getRandomStringNoZero(int length)
  {
    String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < length; i++)
    {
      int number = random.nextInt(base.length());
      sb.append(base.charAt(number));
    }
    return sb.toString();
  }

  // 生成指定长度的随机字符串
  public static String getRandomString(int length)
  {
    String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < length; i++)
    {
      int number = random.nextInt(base.length());
      sb.append(base.charAt(number));
    }
    return sb.toString();
  }

  // 生成指定长度的随机字符串
  public static String getRandomString2(int length)
  {
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < length; i++)
    {
      int number = random.nextInt(3);
      long result = 0;
      switch (number)
      {
        case 0:
          result = Math.round(Math.random() * 25 + 65);
          sb.append(String.valueOf((char) result));
          break;
        case 1:
          result = Math.round(Math.random() * 25 + 97);
          sb.append(String.valueOf((char) result));
          break;
        case 2:
          sb.append(String.valueOf(new Random().nextInt(10)));
          break;
      }
    }
    return sb.toString();
  }

  public static void main(String[] args)
  {
    for (int i = 0; i < 1; i++)
    {
      System.out.println(RandomNumHelper.checkPassword("Abc1"));
    }

  }

}
